var ztree;
var vm = new Vue({
    el : '#rrapp',
    data : {
        showList : true,
        title : null,
        role : {}
    },
    methods : {
        query : function() {
            vm.reload();
        },
        getMenuTree : function(roleId) {
            // 加载菜单树
            $.get(rootPath + "/res/res_do/reslists.do", function(r) {
                ztree = $.fn.zTree.init($("#menuTree"), setting, r.menuList);
                // 展开所有节点
                ztree.expandAll(true);
                if (roleId != null) {
                    vm.getRole(roleId);
                }
            });
        },
        add : function() {
            vm.showList = false;
            vm.title = "新增角色";
            vm.role = {};
            vm.getMenuTree(null);
        },
        update : function() {
            var roleId = getSelectedRow('roletable', 'id');
            if (roleId == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改角色";
            vm.getRole(roleId);
            vm.getMenuTree(roleId);
        },
        del : function(event) {
            var roleIds = getSelectedRows('roletable', 'id');
            if (roleIds == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function() {
                $.ajax({
                    type : "POST",
                    url : rootPath + '/role/role_do/remove.do',
                    dataType : "json",
                    data : {
                        ids : roleIds
                    },
                    success : function(r) {
                        if (r.success) {
                            alert(r.msg);
                            vm.reload();
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getRole : function(roleId) {
            $.get(rootPath + '/role/role_do/get.do?id=' + roleId, function(r) {
                vm.role = r.role;
                // 勾选角色所拥有的菜单
                var menuIds = vm.role.menuIdList;
                for (var i = 0; i < menuIds.length; i++) {
                    var node = ztree.getNodeByParam("id", menuIds[i]);
                    ztree.checkNode(node, true, false);
                }
            });
        },
        saveOrUpdate : function() {
            var url = vm.role.id == null ? rootPath + "/role/role_do/create.do" : rootPath + "/role/role_do/update.do";
            var nodes = ztree.getCheckedNodes(true);
            var menuIdList = new Array();
            for (var i = 0; i < nodes.length; i++) {
                menuIdList.push(nodes[i].id);
            }
            vm.role.menuIdList = menuIdList;
            $.ajax({
                type : "POST",
                url : url,
                contentType : "application/json",
                data : JSON.stringify(vm.role),
                success : function(data) {
                    
                    // vm.showList = true;
                    alert(data.msg);
                }
            });
        },
        reload : function() {
            vm.showList = true;
            layui.use('table', function() {
                var table = layui.table;
                table.render({
                    elem : '#roletable' // 选定是那个DIV
                    ,
                    url : rootPath + '/role/role_do/list.do',
                    cols : [
                        [
                            {
                                type : 'checkbox'
                            }, {
                                field : 'id',
                                width : 180,
                                title : 'ID'
                            }, {
                                field : 'name',
                                width : 180,
                                title : '角色名'
                            }, {
                                field : 'state',
                                title : '状态',
                                minWidth : 100,
                                templet : function(data) {
                                    if (data.state == 0) {
                                        return "正常";
                                    }
                                    if (data.state == 1) {
                                        return "<font color='red'>刪除</font>";
                                    }
                                    return "数据异常";
                                }
                            } ] ],
                    page : true, // 开启分页
                    request : laypagerequest,
                    response : laypageresponse,
                    where : $("#searchForm").serializeJSON()
                });
            });
        }
    }
});
var setting = {
    data : {
        simpleData : {
            enable : true,
            idKey : "id",
            pIdKey : "parentid",
            rootPId : "0"
        },
        key : {
            url : "nourl"
        }
    },
    check : {
        enable : true,
        nocheckInherit : true
    }
};
vm.reload();
